package com.avic.rong.goodsSort.activity.secondListActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.avic.rong.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsWebActivity extends AppCompatActivity {

    String url;
    @BindView(R.id.shop_webView)
    WebView shopWebView;

    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//            Log.d(TAG, "======shouldOverrideUrlLoading: " + view.getUrl());
            if (view.getUrl().equals("https://www.google.com/")) {
                // ！！！=======如果写在这里将永远得不到执行，访问国外网站根本不回调shouldOverrideUrlLoading
                Toast.makeText(GoodsWebActivity.this, "国内无法访问国外的网站", Toast.LENGTH_SHORT).show();
                return true;// 停止继续加载该url
            }
            return super.shouldOverrideUrlLoading(view, request);// 默认返回false继续加载
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {// 可能涉及重定向导致再次调用此方法
//            Log.d(TAG, "========onPageStarted: " + url);
//            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
//            Log.d(TAG, "========onPageFinished: ");
//            progressBar.setVisibility(View.GONE);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_web);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");

        WebSettings webSettings = shopWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);// 加上这句很重要，否则加载不全网页效果

        shopWebView.setWebViewClient(webViewClient);
        shopWebView.loadUrl(url);

    }
}
